<?php

namespace App\Http\Controllers\Mobile;

use App\Models\BlockItem;
use App\Models\ItemPush;
use App\Models\Shop;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class IndexController extends BaseController
{
    public function index(){

        $focusImages = BlockItem::where('block_id', 13)->orderBy('displayorder')->get();
        $jingXuan = BlockItem::where('block_id', 5)->limit(4)->orderBy('displayorder')->get();

        $chaoZhi = Cache::remember('chaoZhi', 5, function (){
            $items = ItemPush::with('item')->whereHas('item', function ($query){
                return $query->onSale();
            })->where('groupid', 4)->limit(6)->get()->map(function ($item){
                return $item->item;
            });
            return $items;
        });

        $youxuanList = Cache::remember('youxuanList', 5, function (){
            $items = ItemPush::with('item')->whereHas('item', function ($query){
                return $query->onSale();
            })->where('groupid', 3)->get()->map(function ($item){
                return $item->item;
            });
            return $items;
        });

        $this->assign(compact('focusImages', 'chaoZhi', 'jingXuan', 'youxuanList'));

        return $this->view('mobile.index');
    }

    public function food()
    {
        $shopIds = [1838,1633];
        $shops = [];
        Shop::whereIn('shop_id', $shopIds)->get()->map(function ($shop) use (&$shops){
            $shop->logo = image_url($shop->logo);
            $shop->items = [];
            $shops[$shop->shop_id] = $shop->toArray();
        });

        $prefix = DB::getTablePrefix();
        $query = DB::table(DB::raw($prefix."item as i"))
            ->where(DB::raw("(select count(*) from {$prefix}item where shop_id=i.shop_id and itemid<i.itemid)"),'<', 3);

        //echo $query->toSql();exit();
        $query->whereIn('shop_id', $shopIds)->orderByDesc('itemid')->get()->map(function ($item) use (&$shops){
            $item->thumb = image_url($item->thumb);
            $item->image = image_url($item->image);
            $shops[$item->shop_id]['items'][] = get_object_vars($item);
        });

        return $this->view('mobile.food', ['shops'=>$shops]);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function youxuan(){
        $items = ItemPush::with('item')->has('item')->where('groupid', 3)->orderByDesc('id')->limit(50)
            ->get()->map(function ($item){
                return $item->item;
            });
        return $this->view('mobile.youxuan', compact('items'));
    }

    public function fupin()
    {

        $focusImages = BlockItem::where('block_id', 11)->orderBy('displayorder')->get();
        $jingxuan = BlockItem::where('block_id', 12)->orderBy('displayorder')->get();
        $items = ItemPush::with('item')->has('item')->orderByDesc('id')->limit(50)
            ->get()->map(function ($item){
                return $item->item;
            });

        return $this->view('mobile.fupin', compact('focusImages', 'jingxuan', 'items'));
    }
}
